#include "movieshow.h"
#include "ui_movieshow.h"

extern QHostAddress DeviceHost;

MovieShow::MovieShow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MovieShow)
{
    ui->setupUi(this);
    photo_flag=false;
    video_flag=false;
    udpSocket=new QUdpSocket(this);

    //QHostAddress host("");

    timer=new QTimer(this);
    timer->setInterval(10);
    timer->setSingleShot(false);

    connect(ui->pushButton_monitor,SIGNAL(clicked()),this,SLOT(btnMonitorClickedSlot()));
    connect(ui->pushButton_photograph,SIGNAL(clicked()),this,SLOT(btnPhotographClickedSlot()));
    connect(ui->page_down,SIGNAL(clicked()),this,SLOT(pageDownClickedSlot()));
    connect(ui->page_up,SIGNAL(clicked()),this,SLOT(pageUpClickedSlot()));
    connect(ui->save_image,SIGNAL(clicked()),this,SLOT(saveImageSlot()));
    connect(ui->delete_image,SIGNAL(clicked()),this,SLOT(deleteImageSlot()));
    connect(timer,SIGNAL(timeout()),this,SLOT(timeOutSlot()));
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(readyReadSlot()));
}

MovieShow::~MovieShow()
{
    delete ui;
}

void MovieShow::setLabelImage()
{
    QPixmap pixmap=pic_list.at(label_index);
    pixmap = pixmap.scaled(ui->label_video->size(), Qt::KeepAspectRatio);
    ui->label_picture->setScaledContents(true);
    ui->label_picture->setPixmap(pixmap);
}

void MovieShow::btnMonitorClickedSlot()
{
    timer->start();
    timeOutSlot();
    video_flag=true;
}

void MovieShow::btnPhotographClickedSlot()
{
    if(!video_flag)
    {
        QMessageBox::critical(this,"提示","没有视频流");
        return;
    }
    QString message = "get_photo";
    udpSocket->writeDatagram(message.toUtf8(), DeviceHost, VIDEO_PORT);
    photo_flag=true;
}

void MovieShow::readyReadSlot()
{
    // 从套接字中读取所有数据
    while (udpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        // 动态设置大小为第一个挂起的 udp 数据包的大小
        datagram.resize(udpSocket->pendingDatagramSize());
        // 读取数据
        udpSocket->readDatagram(datagram.data(), datagram.size());

        QImage image;
        if (image.loadFromData(datagram)) {
            image=image.mirrored(true, false);
            // 成功加载图像后，显示图像
            QPixmap pixmap = QPixmap::fromImage(image);
            pixmap = pixmap.scaled(ui->label_video->size(), Qt::KeepAspectRatio);
            //ui->label_video->setScaledContents(true);
            ui->label_video->setPixmap(pixmap);
            if(photo_flag)
            {
                pic_list.prepend(pixmap);
                pixmap = pixmap.scaled(ui->label_video->size(), Qt::KeepAspectRatio);
                //ui->label_picture->setScaledContents(true);
                ui->label_picture->setPixmap(pixmap);
                label_index=0;
                photo_flag=false;
            }
        }

    }
}

void MovieShow::timeOutSlot()
{
    QString message = "get_stream";
    udpSocket->writeDatagram(message.toUtf8(), DeviceHost, VIDEO_PORT);
}

void MovieShow::saveImageSlot()
{
    if(label_index==-1)
    {
        QMessageBox::critical(this,"提示","没有截图");
        return;
    }
    // 获取当前日期和时间并将其格式化为字符串格式
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMdd-hhmmss");
    // 将文件名设置为当前时间
    QString fileName = "image-" + dateTime + ".png";
    // 打开文件对话框以选择文件保存路径
    QString filePath = QFileDialog::getSaveFileName(nullptr, "Save Image", fileName, "Images (*.png *.bmp *.jpg)");
    // 在目录中保存图像到文件
    QImageWriter writer(filePath);
    writer.setFormat("PNG"); // 指定图像格式+-------------+-
    writer.write(ui->label_picture->pixmap()->toImage());
}

void MovieShow::pageDownClickedSlot()
{
    if(label_index==-1)
    {
        QMessageBox::critical(this,"提示","没有截图");
        return;
    }
    else
    {
        if(label_index==pic_list.size()-1)
        {
            label_index=0;
        }
        else
        {
            label_index++;
        }
    }
    setLabelImage();
}

void MovieShow::pageUpClickedSlot()
{
    if(label_index==-1)
    {
        QMessageBox::critical(this,"提示","没有截图");
        return;
    }
    else
    {
        if(label_index==0)
        {
            label_index=pic_list.size()-1;
        }
        else
        {
            label_index--;
        }
    }
    setLabelImage();
}

void MovieShow::deleteImageSlot()
{
    if(label_index==-1)
    {
        QMessageBox::critical(this,"提示","没有截图");
        return;
    }
    if(label_index==pic_list.length()-1)
    {
        pic_list.removeAt(label_index);
        label_index--;
    }
    else
    {
        pic_list.removeAt(label_index);
    }
    if(pic_list.isEmpty())
    {
        ui->label_picture->clear();
        label_index=-1;
    }
    else
    {
        setLabelImage();
    }
}
